package com.example.douyintest;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.ToastUtils;
import com.levine.abllib.AblConfig;
import com.levine.abllib.AblService;
import com.levine.abllib.AblStepHandler;
import com.levine.abllib.AblSteps;
import com.levine.abllib.utils.AblUtil;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AblConfig.Builder()
                .setMonitoringPackageNames("com.ss.android.ugc.aweme")
                .setLogTag("DDD")//logtag，不设置默认是abllib
                .setStepMsgDelayMillis(2000)//步骤延迟时间
                .setFindViewMillisInFuture(10000)//寻找界面超时时间
                .setFindViewCountDownInterval(200)//寻找界面间隔时间
                .build().init();
        AblStepHandler.getInstance().initStepClass(new TestAblStep1());
        findViewById( R.id.float_start).setOnClickListener(v -> AblUtil.openAccessibilitySettings());
        findViewById( R.id.btn_1 ).setOnClickListener(v -> AblUtil.addSuspensionWindowView(MainActivity.this, initWindowView()));
        findViewById( R.id.btn_2 ).setOnClickListener(v -> AblUtil.openDrawOverlaysAnth(MainActivity.this));
        findViewById( R.id.btn_3 ).setOnClickListener(v -> {
            if (AblUtil.isAccessibilityServiceOpen(MainActivity.this)) {
                AblStepHandler.getInstance().setStop(false);
                AblStepHandler.sendMsg(AblSteps.STEP_1);

            } else {
                ToastUtils.showShort("请先开启辅助服务");
            }
        });
    }

    @NonNull
    private View initWindowView(){
        View view = View.inflate(this, R.layout.view_test, null);
        view.findViewById(R.id.fu_btn_1).setOnClickListener(v -> {
            if (AblUtil.isAccessibilityServiceOpen(MainActivity.this)) {
                AblStepHandler.getInstance().setStop(false);
                AblStepHandler.sendMsg(AblSteps.STEP_1);
            } else {
                ToastUtils.showShort("请先开启辅助服务");
            }
        });
        view.findViewById(R.id.fu_btn_2).setOnClickListener(v -> {
            WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
            windowManager.removeView(view);
        });
        view.findViewById(R.id.fu_btn_3).setOnClickListener(v -> AblStepHandler.getInstance().setStop(true));
        view.findViewById(R.id.fu_btn_4).setOnClickListener(v -> {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                AblService.getInstance().disableSelf();
            }
        });

        return view;
    }

}